<?php
ptaste();
require_once(dirname(__FILE__)."/utils.php");
class JEngMailer extends JEngUtilHelper
{
	public $msubject;
	public $mto;
	public $mbody;
	public $mfrom;
	public $mreplyto;

	function __construct($utils)
	{
		parent::__construct($utils);
		$this->setFrom("automail@somesite","Автоматическая рассылка somesite");
	}
	
	function setFrom($from,$user)
	{
	  if (!empty($user))
	    $from=$user." <".$from.">";
	  $this->mfrom=$from;
	  $this->mreplyto=$from;
	}
	
	function setReplyTo($replyto,$user)
	{
	  if (!empty($user))
	    $replyto=$user." <".$replyto.">";
	  $this->mreplyto=$replyto;
	}
	
	function sendMail($subject,$to="",$body="",$from="",$replyto="")
	{
	  $this->newMail($subject,$to,$body);
	  if (!empty($from))  $this->setFrom($from);
	  if (!empty($replyto))  $this->setReplyTo($replyto);
	  return $this->send();
	}
	
	function newMail($subject,$to="",$body="")
	{
	  $this->msubject=$subject;
	  $this->mto=$to;
	  $this->mbody=$body;
	}
	
	function send()
	{
		if (empty($this->mto) || empty($this->msubject) || empty($this->mbody))
		  return 1;
		$this->utils()->site()->logger()->info("Sending mail %s to %s",$this->msubject,$this->mto);
		$headers="From: ".$this->mfrom."\r\n".
			"Reply-to: ".$this->mreplyto."\r\n".
			"X-Mailer: PHP/".phpversion();
			;
		if (!mail($this->mto,$this->msubject,$this->mbody,$headers))
			return 2;
		return 0;
	}
	
	function setTo($to,$user="")
	{
	  if (!empty($user))
	    $to=$user." <".$to.">";
	  $this->mto=$to;
	}
	
	function addTo($to,$user="")
	{
	  if (!empty($user))
	    $to=$user." <".$to.">";
	  if (empty($this->mto))
	    $this->setTo($to,$user);
	  $this->mto.=",".$to;
	}
	
	function setBody($body)
	{
	  $this->mbody=$body;
	}
	
	function addBody($body)
	{
	  $this->mbody.=$body;
	}
	
	function body()
	{
	  return $this->mbody;
	}
}

	
?>